import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * @projectName: java-daily-questions
 * @package: PACKAGE_NAME
 * @className: CollectionsRemoveOperation
 * @author: ZM
 * @description: 集合的删除操作
 * @date: 2021/8/31 9:45
 * @version: 1.0
 */
public class CollectionsRemoveOperation {

    public static void main(String[] args) {

        List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 3));
        list.forEach(System.out::print);
        System.out.println();
        // 1、反向循环遍历
        for (int i = list.size() - 1; i >= 0; i--) {
            if(3 == list.get(i)) {
                list.remove(i);
            }
        }
        list.forEach(System.out::print);

        // 2、迭代器遍历删除
        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()) {
            Integer i = iterator.next();
            if (3 == i) {
                iterator.remove();
            }
        }
        list.forEach(System.out::print);

        // 3、正向遍历删除
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) == 3) {
                list.remove(i--);
            }
        }
        list.forEach(System.out::print);

        // 4、removeIf
        list.removeIf(e -> 3 == e);
        list.forEach(System.out::println);
    }
}
